/**
 * Created by wangshaohua on 2018/1/25.
 */
moduleContent.controller("ContentArticleDetail", function (ExerciseContentResource, LanguageService, UploadFileService, $$CambioConfig, ContentDynamicHtmlResource, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        //$scope.reset();
        pageOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('内容课程详细'));
        $scope.contentId = $stateParams.contentId;
        $scope.loadArticle();
    });

    // ------------------------------------------------------------------------------------------------------

    $scope.loadArticle = function () {
        summer.init('content');
        ExerciseContentResource.findById({id: $scope.contentId}, {}, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.currentContentArticle = queryResult.data;
            summer.setValue('content', $scope.currentContentArticle.content);
        });
    }

    $scope.save = function () {
        Alert.confirm(function () {
            ExerciseContentResource.updateArticle({}, $scope.currentContentArticle, function (result) {
                Alert.success();
                $scope.loadArticle();
            });
        })
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------
    $scope.uploadPicture = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.currentContentArticle.picture = o.id;
            });
        }
    };
});